<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <form class="layui-form">
        <div class="layui-form lay-header">
          <div class="toolbar">
            <div class="layui-form-item lay-query-content">
              <div class="lay-query-box">
                <div class="query-lt">
                  <!-- <div class="layui-inline">
                    <label class="layui-form-label w-auto">工序：</label>
                    <div class="layui-input-inline">
                      <select name="processId" id="selectProcessId">
                        <option value="">选择工序</option>
                      </select>
                    </div>
                  </div> -->
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto"
                      >质检任务编号：</label
                    >
                    <div class="layui-input-inline">
                      <input
                        name="code"
                        class="layui-input"
                        type="text"
                        placeholder="请输入质检任务编号"
                      />
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">开工单编号：</label>
                    <div class="layui-input-inline">
                      <input
                        name="workOrderCode"
                        class="layui-input"
                        type="text"
                        placeholder="请输入开工单编号"
                      />
                    </div>
                  </div>
                  <!-- <div class="layui-inline">
                    <label class="layui-form-label w-auto">产品类别：</label>
                    <div class="layui-input-inline">
                      <div
                        style="margin-left: 33px; width: 84%"
                        id="productTypeName"
                        lay-filter="productTypeName"
                      ></div>
                      <input
                        id="productTypeId"
                        name="productTypeId"
                        type="hidden"
                      />
                    </div>
                  </div> -->
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">任务状态：</label>
                    <div class="layui-input-inline">
                      <select name="">
                        <option value="">请选择</option>
                        <option value="inspect">送检中</option>
                        <option value="finish">完成</option>
                        <option value="confirm">待认领</option>
                        <option value="close">关闭</option>
                      </select>
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">报工单编号：</label>
                    <div class="layui-input-inline">
                      <input
                              name="reportOrderCode"
                              class="layui-input"
                              type="text"
                              placeholder="请输入报工单编号"
                      />
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">质检分类：</label>
                    <div class="layui-input-inline">
                      <select name="qualityTestingCategoryId">
                        <option value="">全部</option>
                        <option value="1">化学检测</option>
                        <option value="2">外观检测</option>
                        <option value="3">物理检测</option>
                        <option value="4">其他检测</option>
                      </select>
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">质检类型：</label>
                    <div class="layui-input-inline">
                      <select name="qcQualityTypeId">
                        <option value="">全部</option>
                        <option value="1">来料质检(IQC)</option>
                        <option value="2">工序质检(IPQC)</option>
                        <option value="3">成品质检(FQC)</option>
                        <option value="4">出库检</option>
                      </select>
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">任务添加时间：</label>
                    <div class="layui-input-inline">
                      <input name="createTime" id="createTime" class="layui-input date-icon" type="text"
                             placeholder="请选择任务添加时间" style="width: 200px" readonly/>
                    </div>
                    <input name="startCreateTime" id="sCreateTime" type="hidden"/>
                    <input name="endCreateTime" id="eCreateTime" type="hidden"/>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">送检人：</label>
                    <div class="layui-input-inline">
                      <input
                              name="inspector"
                              class="layui-input"
                              type="text"
                              placeholder="请输入送检人"
                      />
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">认领人：</label>
                    <div class="layui-input-inline">
                      <input
                              name="getName"
                              class="layui-input"
                              type="text"
                              placeholder="请输入认领人"
                      />
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">车间：</label>
                    <div class="layui-input-inline">
                      <input
                              name="workshopName"
                              class="layui-input"
                              type="text"
                              placeholder="请输入车间"
                      />
                    </div>
                  </div>
                </div>
                <div class="query-rt">
                  <div class="layui-inline">
                    <button
                      type="button"
                      class="layui-btn icon-btn color-green"
                      lay-filter="formSubSearchLog"
                      lay-submit
                    >
                      查询
                    </button>
                    <button
                      class="layui-btn icon-btn color-white"
                      lay-filter="resets"
                      lay-submit
                      type="button"
                    >
                      重置
                    </button>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="lay-btn-rows">
            <div class="lay-btn-box">
              <div class="lay-btn-lt">
                <!--                                <button id="workOrderQualityTestingTaskBtnAdd" class="layui-btn icon-btn color-reseda"-->
                <!--                  1                      type="button">-->
                <!--                                    添加质检任务-->
                <!--                                </button>-->
              </div>
              <div class="lay-btn-rt"></div>
            </div>
          </div>
        </div>
      </form>
      <!-- 表格 -->
      <table
        class="layui-table"
        id="workOrderQualityTaskTable"
        lay-filter="workOrderQualityTaskTable"
      ></table>
    </div>
  </div>
</div>

<style>
  div[lay-id="addQualityInspectionTaskTable"] .layui-form,
  div[lay-id="addQualityInspectionTaskTable"] .layui-table-box,
  div[lay-id="addQualityInspectionTaskTable"] .layui-table-body,
  div[lay-id="addQualityInspectionTaskTable"] .layui-table-cell {
    overflow: visible;
  }

  div[lay-id="addQualityInspectionTaskTable"] .layui-table-cell {
    padding: 10px;
    height: auto;
  }

  xm-select {
    margin-left: -21%;
    width: 190px;
    min-height: 32px;
    line-height: 32px;
  }
</style>

<!-- 表格操作列 -->
<script type="text/html" id="workOrderQualityTestingTaskTableBar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="details"
    >详情</a
  >
  <!-- {{# if(d.status!== 'finish'){}} -->
  <!-- <a class="layui-btn layui-btn-xs" lay-event="check">检测</a> -->
  <a class="layui-btn layui-btn-xs" lay-event="check1">检测</a>
  <!-- {{# } }} -->
</script>
<!--<script type="text/html" id="workOrderQualityTestingTaskTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="details">详情</a>
    {{# if(d.status !== 'finish'){ }}
    <a class="layui-btn layui-btn-xs" lay-event="check">检测</a>
    {{# } }}
</script>-->
<!--添加质检任务弹窗-->
<script id="addQualityInspectionTaskTemplate" type="text/html">
  <form
    lay-filter="addQualityInspectionTaskForm"
    class="layui-form model-form form-scroll"
  >
    <div class="open-details-box pd-20">
      <input name="id" type="hidden" />
      <div
        class="layui-form-item"
        style="margin: 20px 0;"
        id="productionPlanIdBox"
      >
        <label class="layui-form-label w-auto">选择开工单：</label>
        <div class="layui-input-inline">
          <input
            name="productionPlanId"
            id="productionPlanId"
            class="layui-input"
            type="text"
            placeholder="请选择开工单"
            readonly="readonly"
            style="cursor: pointer;"
          />
        </div>
      </div>
      <div
        class="layui-form-item"
        id="addQualityInspectionTaskTableBox"
        style="display: none;"
      >
        <div class="open-details-title">开工单信息</div>
      </div>
      <div
        class="layui-form-item"
        id="addQualityInspectionTaskRemarkBox"
        style="display: none;"
      >
        <label class="layui-form-label w-auto">送检备注说明：</label>
        <div class="layui-input-block" style="margin-left: 130px">
          <textarea
            placeholder="送检备注说明"
            name="remark"
            style="resize: none; padding: 10px;width: 100%;min-height: 80px;box-sizing: border-box"
          ></textarea>
        </div>
      </div>
    </div>
    <div class="layui-form-item text-right pd-20">
      <button
        class="layui-btn layui-btn-primary"
        type="button"
        ew-event="closeDialog"
      >
        取消
      </button>
      <button
        class="layui-btn"
        lay-filter="addQualityInspectionTaskSubmit"
        lay-submit
        style="display: none;"
      >
        提交
      </button>
    </div>
  </form>
</script>

<script type="text/html" id="addQualityInspectionTaskTableBar">
  <a class="layui-btn  layui-btn-xs" lay-event="print">打印标签</a>
</script>

<!--选择开工单-->
<script type="text/html" id="selectTheWorkProcessTemplate">
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
        <!-- 头部操作栏 -->
        <div class="layui-form toolbar">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label w-auto">开工单编号：</label>
              <div class="layui-input-inline mr0" style="width: 240px;">
                <input
                  name="code"
                  class="layui-input"
                  type="text"
                  placeholder="开工单编号"
                />
              </div>
            </div>
            <div class="layui-inline">
              <button
                class="layui-btn icon-btn"
                lay-filter="selectTheWorkProcessFormSubSearchLog"
                lay-submit
                style="padding: 0 24px;"
              >
                搜索
              </button>
              <button id="selectTheWorkProcessClose" class="layui-btn icon-btn">
                确认选择
              </button>
            </div>
          </div>
        </div>
        <!-- 表格 -->
        <table
          class="layui-table"
          id="selectTheWorkProcessTable"
          lay-filter="selectTheWorkProcessTable"
        ></table>
      </div>
    </div>
  </div>
</script>

<!-- js部分 -->
<script>
  layui.use(["layer", "form", "table", "admin", "laydate"], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var upload = layui.upload;
    var laydate = layui.laydate;

    form.render("select");

    laydate.render({
      elem: "#createTime",
      type: "date",
      range: true,
      done: function (value, date, endDate) {
        if (value) {
          var dateValue = value.split(" - ");
          $("input[name='startCreateTime']").val(dateValue[0] + " 00:00:00");
          $("input[name='endCreateTime']").val(dateValue[1] + " 23:59:59");
        } else {
          $("input[name='startCreateTime']").val("");
          $("input[name='endCreateTime']").val("");
        }
      },
    });
    
    //初始化产品类别 1
    admin.initDataSelectTree(
      true,
      "enterprise/product/admin/productType/getTreeData",
      { materialNatureList: [2, 3, 7] },
      "productTypeName",
      null,
      "物料分类",
      "productTypeId",
      null
    );

    // 渲染表格
    var insTb = table.render({
      elem: "#workOrderQualityTaskTable",
      url: config.base + "production/quality/admin/myWorkOrderQualityTask/list",
      page: true,
      cellMinWidth: 100,
      cols: [
        [
          { type: "numbers", title: "序号" },
          { field: "code", title: "质检任务单编码", width: 158 },
          // { field: "processName", title: "所属计划" },
          { field: "workOrderCode", title: "所属开工单", width: 158 },
          { field: "reportOrderCode", title: "所属报工单", width: 158 },

          {
            field: "qualityAttributes",
            title: "质检属性",
            templet: (d) => {
              return d.qualityAttributes == "1" ? "一物一检" : "简单检测";
            },
          },
          { field: "testingCategoryName", title: "质检分类" },
          { field: "testingTypeName", title: "质检性质" },
          { field: "qcQualityTypeName", title: "质检类型" },
          // { field: "skuCode", title: "物料编码" },
          { field: "submissionQuantity", title: "送检数量" },
          { field: "detectionQuantity", title: "抽检数量" },
          { field: "qualifiedQuantity", title: "合格数" },
          { field: "notQuantity", title: "不合格数" },
          { field: "okRate", title: "合格率(%)" },

          { field: "statusDesc", title: "状态" },
          { field: "creatorName", title: "报验人" },
          { field: "createTime", title: "报验时间" },
          {
            align: "center",
            toolbar: "#workOrderQualityTestingTaskTableBar",
            title: "操作",
            width: 240,
            fixed: "right",
          },
        ],
      ],
      done: function (res, curr, count) {
        //移除按钮
      },
    });

    // 工序名称
    // admin.req(
    //   "enterprise/product/admin/processInfo/getAllProcessInfo",
    //   {},
    //   function (res) {
    //     if (res.code == 200) {
    //       $.each(res.data, function (index, item) {
    //         $("#selectProcessId").append(new Option(item.nodeNames, item.id));
    //       });
    //       form.render("select");
    //     } else {
    //       layer.msg(res.msg, { icon: 2 });
    //     }
    //   },
    //   "GET"
    // );
    // 工具条点击事件
    table.on("tool(workOrderQualityTaskTable)", function (obj) {
      let data = obj.data;
      if (obj.event === "details") {
        showQualityInspectionTaskModel(data, "details");
      } else if (obj.event === "print") {
        doPrint(obj);
      } else if (obj.event === "check") {
        showQualityInspectionTaskModel(data, "check");
      } else if (obj.event === "check1") {
        showQualityInspectionTaskModel(data, "check1");
      }
    });

    function showEditModel(data) {
      if (data) {
        admin.putTempData("taskQualityRecordData", data);
      } else {
        admin.putTempData("taskQualityRecordData", null);
      }
      admin.formOpen({
        type: 1,
        area: ["80%", "92%"],
        offset: ["7%", "17%"],
        style: "font-size: 20px;",
        title: data ? "修改质检记录表" : "添加质检记录表",
        path: "components/res/quality/taskQualityRecordForm.html",
        finish: function () {
          table.reload(insTb, {});
        },
      });
    }

    //监听排序
    table.on("sort(workOrderQualityTaskTable)", function (obj) {
      table.reload("workOrderQualityTaskTable", {
        initSort: obj,
        where: {
          sort: obj.field,
          order: obj.type,
        },
      });
    });

    // 搜索
    form.on("submit(formSubSearchLog)", function (data) {
      insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
    });

    // 重置搜索
    form.on("submit(resets)", function (data) {
      insTb.reload(
        { where: admin.resetSearch(data.field), page: { curr: 1 } },
        "data"
      );
      return false;
    });

    // 添加质检任务弹窗 && 质检任务详情弹窗
    function showQualityInspectionTaskModel(data, _type) {
      //如果是详情 或者是 修改
      if (data) {
        data.type = _type;
        admin.putTempData("workOrderQuaTaskInfoData", data);
      } else {
        admin.putTempData("workOrderQuaTaskInfoData", null);
      }
      let title = "";
      //data ? (_type === 'details' ? '送检任务详情' : '修改质检任务') : '添加质检任务'
      if (data) {
        if (_type === "details") {
          title = "质检任务详情";
          admin.formOpen({
            type: 1,
            area: ["80%", "82%"],
            style: "font-size: 20px;",
            title: title,
            path: "components/res/quality/workOrderQualityTaskDetailForm.html",
            end: function () {
              table.reload(insTb, {});
            },
          });
        } else if (_type === "check" || _type === "check1") {
          if (data) {
            admin.putTempData("taskQualityRecordData", data);
          } else {
            admin.putTempData("taskQualityRecordData", null);
          }
          admin.formOpen({
            type: 1,
            area: ["80%", "92%"],
            style: "font-size: 20px;",
            title: "质检任务检测",
            path: "components/res/quality/taskQualityRecordForm.html",
            success: function (layero, index) {
              parentData.data = {
                eventName: data.qualityAttributes,
              };
            },
            finish: function () {
              table.reload(insTb, {});
            },
          });
        } else {
          title = "修改质检任务";
          admin.formOpen({
            type: 1,
            area: ["80%", "82%"],
            style: "font-size: 20px;",
            title: title,
            path: "components/res/quality/workOrderQualityTaskForm.html",
            finish: function () {
              table.reload(insTb, {});
            },
          });
        }
      } else {
        title = "添加质检任务";
        admin.formOpen({
          type: 1,
          area: ["80%", "82%"],
          style: "font-size: 20px;",
          title: title,
          path: "components/res/quality/workOrderQualityTaskForm.html",
          finish: function () {
            table.reload(insTb, {});
          },
        });
      }
    }

    // 删除
    function doDelete(obj) {
      layer.confirm(
        "确定要删除吗？",
        {
          offset: "65px",
          skin: "layui-layer-admin",
        },
        function (i) {
          layer.close(i);
          layer.load(2);
          admin.req(
            "production/qualityTesting/admin/workOrderQualityTestingTask/" +
              obj.data.id,
            {},
            function (res) {
              layer.closeAll("loading");
              if (res.code == 200) {
                layer.msg(res.msg, { icon: 1 });
                obj.del();
              } else {
                layer.msg(res.msg, { icon: 2 });
              }
            },
            "DELETE"
          );
        }
      );
    }

    function jsToFormData(config) {
      const formData = new FormData();
      Object.keys(config).forEach((key) => {
        formData.append(key, config[key]);
      });

      return formData;
    }

    // 关闭工单
    function doClose(data) {
      layer.confirm(
        "确定要关闭吗？",
        {
          offset: "65px",
          skin: "layui-layer-admin",
        },
        function (i) {
          layer.close(i);
          layer.load(2);
          admin.req(
            "production/productionPlan/admin/productionWorkOrder/update",
            { id: data.id, status: "close" },
            function (res) {
              layer.closeAll("loading");
              if (res.code == 200) {
                layer.msg(res.msg, { icon: 1 });
                table.reload("workOrderQualityTaskTable");
              } else {
                layer.msg(res.msg, { icon: 2 });
              }
            },
            "put"
          );
        }
      );
    }

    // 导出excel
    $("#workOrderQualityTestingTaskBtnExport").click(function () {
      var checkRows = table.checkStatus("workOrderQualityTaskTable");
      if (checkRows.data.length == 0) {
        layer.msg("请选择要导出的数据", { icon: 2 });
      } else {
        table.exportFile(insTb.config.id, checkRows.data, "xls");
      }
    });

    // 打印
    function doPrint(obj) {
      layer.confirm(
        "确定要打印吗？",
        {
          offset: "65px",
          skin: "layui-layer-admin",
        },
        function (i) {
          layer.close(i);
          layer.load(2);
          admin.req(
            "production/qualityTesting/admin/workOrderQualityTestingTask/printLabels",
            { id: obj.data.id },
            function (res) {
              layer.closeAll("loading");
              if (res.code == 200) {
                layer.msg(res.msg, { icon: 1 });
              } else {
                layer.msg(res.msg, { icon: 2 });
              }
            },
            "get"
          );
        }
      );
    }
  });
</script>
